package com.iseastar.guojiang.transfer;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.iseastar.guojiang.dialog.DialogMgr;
import com.iseastar.guojiang.model.NewParcelBean;
import com.shouhuobao.station.R;
import com.shouhuobao.take.utils.StringUtils;

import java.util.ArrayList;

import droid.frame.activity.base.BaseAdapterWithImage;

/**
 * 火箭侠卫星仓交接详情列表
 * Created by lidandan on 2018/1/30.
 */
public class ParcelHandoverDetailsListAdapter extends BaseAdapterWithImage<NewParcelBean> {

    private ListView mListView;

    public ParcelHandoverDetailsListAdapter(ArrayList<NewParcelBean> items, Activity context, ListView listView) {
        super(items, context, listView);
        this.mListView = listView;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.parcel_handover_details_item, parent, false);
            holder = new ViewHolder();
            holder.photoImg = (ImageView) convertView.findViewById(R.id.photo_img);
            holder.phoneTV = (TextView) convertView.findViewById(R.id.phone_tv);
            holder.codeTV = (TextView) convertView.findViewById(R.id.code_tv);
            holder.timeTV = (TextView) convertView.findViewById(R.id.time_tv);
            holder.statusTV = (TextView) convertView.findViewById(R.id.status_tv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final NewParcelBean item = getItem(position);
        setDataToView(holder, item, position);
        return convertView;

    }

    private void setDataToView(ViewHolder viewHolder, final NewParcelBean parcel, final int position) {
        showImage(parcel.getParcelImgUrl(), viewHolder.photoImg, null);
        viewHolder.photoImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!StringUtils.isEmpty(parcel.getParcelImgUrl())) {
                    DialogMgr.showLargeStationPhoto(context, parcel.getParcelImgUrl());
                }
            }
        });
        viewHolder.phoneTV.setText("下单人手机号：" + parcel.getUserPhone());
        if (StringUtils.isEmpty(parcel.getBagCode())) {
            viewHolder.codeTV.setText("投递编号：无");
        } else {
            viewHolder.codeTV.setText("投递编号：" + parcel.getBagCode());
        }
        viewHolder.timeTV.setText("下单时间：" + droid.frame.utils.sqlite.DateUtils.format(parcel.getCreateTime()));
        viewHolder.statusTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((ParcelHandoverDetailsListActivity) context).updateParcelStatus(position);
            }
        });
        //破损
        if (parcel.getParcelStatus() == 2) {
            viewHolder.statusTV.setText("破损");
            viewHolder.statusTV.setTextColor(Color.parseColor("#FF001F"));
            Drawable drawable = context.getResources().getDrawable(R.drawable.courier_handover_warehous_ff001f_shape);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            viewHolder.statusTV.setCompoundDrawables(drawable, null, null, null);
            viewHolder.statusTV.setCompoundDrawablePadding(5);
        } else if (parcel.getParcelStatus() == 3) {
            viewHolder.statusTV.setText("缺失");
            viewHolder.statusTV.setTextColor(Color.parseColor("#FF001F"));
            Drawable drawable = context.getResources().getDrawable(R.drawable.courier_handover_warehous_ff001f_shape);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            viewHolder.statusTV.setCompoundDrawables(drawable, null, null, null);
            viewHolder.statusTV.setCompoundDrawablePadding(5);
        } else if (parcel.getParcelStatus() == 4) {
            viewHolder.statusTV.setText("其他");
            viewHolder.statusTV.setTextColor(Color.parseColor("#FF9500"));
            Drawable drawable = context.getResources().getDrawable(R.drawable.courier_handover_warehou_ff9500_shape);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            viewHolder.statusTV.setCompoundDrawables(drawable, null, null, null);
            viewHolder.statusTV.setCompoundDrawablePadding(5);
        } else {
            viewHolder.statusTV.setText("完整");
            viewHolder.statusTV.setTextColor(Color.parseColor("#6783D8"));
            Drawable drawable = context.getResources().getDrawable(R.drawable.courier_handover_warehous_shape);
            /// 这一步必须要做,否则不会显示.
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            viewHolder.statusTV.setCompoundDrawables(drawable, null, null, null);
            viewHolder.statusTV.setCompoundDrawablePadding(5);
        }
    }

    class ViewHolder {

        private ImageView photoImg;
        private TextView phoneTV;
        private TextView codeTV;
        private TextView timeTV;
        private TextView statusTV;
    }

    /**
     * 更新局部item
     *
     * @param position
     * @param parcelBean
     */
    public void updateItemView(int position, NewParcelBean parcelBean) {
        //得到第一个可显示控件的位置，
        int visiblePosition = mListView.getFirstVisiblePosition();
        int visibleLastPosi = mListView.getLastVisiblePosition();
        //只有当要更新的view在可见的位置时才更新，不可见时，跳过不更新
        if (position >= visiblePosition && position <= visibleLastPosi) {
            int index = position - visiblePosition;
            if (index >= 0) {
                //得到要更新的item的view
                View view = mListView.getChildAt(index);
                //从view中取得holder
                ViewHolder holder = (ViewHolder) view.getTag();
                //更改状态
                setDataToView(holder, parcelBean, position);
                //直接更改数据源
                getItems().remove(position);
                getItems().add(position, parcelBean);
            }
        } else {
            //直接更改数据源
            getItems().remove(position);
            getItems().add(position, parcelBean);
        }
    }
}
